function r = solvemodelmw(p,q,mw)

% include q and minimum wage into parameter structure
p.q = q;
p.mw = mw;

%% check for bounded output (assumption 1)

% compute upper bound for log q
qinfinity = log(integral(@(x) exp((logcapprod(x,p)-p.q)*(p.lambda-1)),0,1)^(1/(1-p.lambda)));

% check whether q < q_infinity and return to script if not
if p.q >= qinfinity-p.qtol
    
    r.exitflag = 0;
    r.exitflag1 = "unbounded output";

    return
    
end

%% check for prohibitive minimum wage

if p.mw > 0
    
    mwtest = (integral(@(x) (max(exp(loglaborprod(1,x,p))./p.mw,exp(logcapprod(x,p)))).^(p.lambda-1),0,1))^(1/(p.lambda-1));

    if mwtest <= 1
    
        r.exitflag = 0;
        r.exitflag1 = "prohibitive minimum wage, zero output";

        return

    end
    
end

%% check whether minimum wage is positive and solve model accordingly
if mw <= 0
    
    if p.lambda <= 0.5 % use optimization-based solution algorithm
        r = solvemodelnew(0,p);
    else % use algorithm based on equilibrium conditions for higher lambda
        r = solvemodel(0,p);
    end
    
    r.qinfinity = qinfinity;
    r.sbar = 0;
    
else
    
    % check for prohibitive minimum wage (approximative test)
    % uncomment the if statement to include the check and avoid error in case
    % of approximately prohibitive minimum wage
    %if mwcondition(0.99,p) < 0
    %    r.exitflag = "approx. prohibitive minimum wage";
    %    return
    %end
    
    % compute sbar
    sbar = fzero(@(s) mwcondition(s,p),[0 0.99],p.optionsfzero);
    
    % solve model with correct sbar
    if p.lambda <= 0.5 % use optimization-based solution algorithm
        r = solvemodelnew(sbar,p);
    else % use algorithm based on equilibrium conditions for higher lambda
        r = solvemodel(sbar,p);
    end
    
    r.qinfinity = qinfinity;
    r.sbar = sbar;
    
end

end